package com.castle.fortress.admin.rule.group.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.member.member.dto.MemberDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.rule.group.entity.RuleGroupEntity;
import com.castle.fortress.admin.rule.group.dto.RuleGroupDto;
import com.castle.fortress.admin.rule.group.mapper.RuleGroupMapper;
import com.castle.fortress.admin.rule.group.service.RuleGroupService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 规则分组表 服务实现类
 *
 * @author
 * @since 2025-04-21
 */
@Service
public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroupEntity> implements RuleGroupService {
    /**
    * 获取查询条件
    * @param ruleGroupDto
    * @return
    */
    public QueryWrapper<RuleGroupEntity> getWrapper(RuleGroupDto ruleGroupDto){
        QueryWrapper<RuleGroupEntity> wrapper= new QueryWrapper();
        if(ruleGroupDto != null){
            RuleGroupEntity ruleGroupEntity = ConvertUtil.transformObj(ruleGroupDto,RuleGroupEntity.class);
            wrapper.like(ruleGroupEntity.getId() != null,"scenario_id",ruleGroupEntity.getId());
            wrapper.like(StrUtil.isNotEmpty(ruleGroupEntity.getTitle()),"title",ruleGroupEntity.getTitle());
            wrapper.like(ruleGroupEntity.getUpdateTime() != null,"update_time",ruleGroupEntity.getUpdateTime());
            wrapper.like(StrUtil.isNotEmpty(ruleGroupEntity.getScenarioSource()),"scenario_source",ruleGroupEntity.getScenarioSource());
            wrapper.like(ruleGroupEntity.getCreateUser() != null,"create_user",ruleGroupEntity.getCreateUser());
            wrapper.like(ruleGroupEntity.getCreateTime() != null,"create_time",ruleGroupEntity.getCreateTime());
            wrapper.like(ruleGroupEntity.getUpdateUser() != null,"update_user",ruleGroupEntity.getUpdateUser());
            wrapper.like(ruleGroupEntity.getIsDeleted() != null,"is_deleted",ruleGroupEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<RuleGroupDto> pageRuleGroup(Page<RuleGroupDto> page, RuleGroupDto ruleGroupDto) {
        QueryWrapper<RuleGroupEntity> wrapper = getWrapper(ruleGroupDto);
        Page<RuleGroupEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<RuleGroupEntity> ruleGroupPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<RuleGroupDto> pageDto = new Page(ruleGroupPage.getCurrent(), ruleGroupPage.getSize(),ruleGroupPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(ruleGroupPage.getRecords(),RuleGroupDto.class));
        return pageDto;
    }


    @Override
    public List<RuleGroupDto> listRuleGroup(RuleGroupDto ruleGroupDto){
        QueryWrapper<RuleGroupEntity> wrapper = getWrapper(ruleGroupDto);
        //TODO 权限预留
        SysUser sysUser = WebUtil.currentUser();
        if (sysUser == null) {
            throw new RuntimeException("未登录，请检查");
        }
        // 使用嵌套条件：查询当前用户创建的规则或系统生成的规则
        wrapper.and(w -> w.eq("create_user", sysUser.getId()).or().eq("scenario_source", "generate"));

        List<RuleGroupEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,RuleGroupDto.class);
    }

    @Override
    public boolean existsByTitle(String title) {
        if (StrUtil.isEmpty(title)) {
            return false;
        }
        QueryWrapper<RuleGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title);
        // 只关心是否有数据，因此使用count查询效率更高
        return baseMapper.selectCount(wrapper) > 0;
    }
}

